<template>
  <div class="app-container">
    <search-form
      v-if="fieldList"
      :fields="fieldList"
      v-show="showSearch"
      @submit="handleQuery"
      @cancel="resetQuery"
    ></search-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-folder-checked"
          size="mini"
          :disabled="multiple"
          @click="handleBatch(1)"
          v-hasPermi="['run:' + moduleInfo.code + ':check']"
          >批量通过</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-folder-delete"
          size="mini"
          :disabled="multiple"
          @click="handleBatch(2)"
          v-hasPermi="['run:' + moduleInfo.code + ':check']"
          >批量不通过</el-button
        >
      </el-col>
      <el-col :span="1.5" v-if="moduleInfo.readonly == 'N' && moduleInfo.importable == 'Y'">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['run:'+moduleInfo.code+':import']"
          >导入</el-button
        >
      </el-col>
      <el-col :span="1.5" v-if="moduleInfo.exportable == 'Y'">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['run:'+moduleInfo.code+':export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <!-- 表格 -->
    <el-table
      @selection-change="handleSelectionChange"
      v-loading="loading"
      :data="dataList"
    >
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="(f, i) in fieldList">
        <el-table-column
          :label="f.label"
          align="center"
          :prop="f.code"
          :key="i"
          v-if="f.list == 'Y' && f.type == 'checkbox'"
        >
          <template slot-scope="scope">
            {{ dictLabel(f.code, scope.row[f.code]) }}
          </template>
        </el-table-column>
        <el-table-column
          :label="f.label"
          align="center"
          :prop="f.code"
          :key="i"
          v-else-if="f.list == 'Y' && f.type == 'radio'"
        >
          <template slot-scope="scope">
            {{ dictLabel(f.code, scope.row[f.code]) }}
          </template>
        </el-table-column>
        <el-table-column
          :label="f.label"
          align="center"
          :prop="f.code"
          :key="i"
          v-else-if="f.list == 'Y' && f.type == 'date'"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row[f.code], "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="f.label"
          align="center"
          :prop="f.code"
          :key="i"
          v-else-if="f.list == 'Y' && f.type == 'datetime'"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row[f.code]) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          v-else-if="f.list == 'Y' && f.type == 'images'"
          :label="f.label"
          align="center"
          :prop="f.code"
          :key="i"
          width="100"
        >
          <template slot-scope="scope">
            <image-preview :src="scope.row[f.code]" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column
          :label="f.label"
          align="center"
          :prop="f.code"
          :key="i"
          v-else-if="f.list == 'Y'"
        />
      </template>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            v-if="scope.row.check_status == '0'"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleCheck(scope.row)"
            v-hasPermi="['run:' + moduleInfo.code + ':check']"
            >审批</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pager.page"
      :limit.sync="pager.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <form-dialog
      v-if="fieldList"
      @submit="submitForm"
      @cancel="cancel"
      :data="formData"
      :action="formAction"
      :title="formTitle"
      :fields="fieldList"
    ></form-dialog>

    <!-- 审批 -->
    <check-dialog
      :applyData="apply"
      @cancel="applyCancel"
      @success="applySuccess"
    />
    
    <!-- 导入 -->
    <import-dialog v-if="moduleInfo"
      :open="upload"
      :model="moduleInfo['api_model']"
      @cancel="importCancel"
      @success="importSuccess"
    />
  </div>
</template>

<script>
import { apiGet, apiPut } from "@/api/openapi";
import ListMixin from "./list";
import CheckDialog from "@/components/ApplyDialog/check";
import FormDialog from "./form";
import ImportDialog from "@/components/ImportDialog";
import SearchForm from "./search";

export default {
  name: "RunFlow",
  components: {
    CheckDialog,
    FormDialog,
    SearchForm,
    ImportDialog
  },
  dicts: [],
  mixins: [ListMixin],
  data() {
    return {};
  },
  methods: {
    handleBatch(status) {
      let data = { asset_flow: [], "@tags": [this.moduleInfo["api_model"]] };
      data[this.moduleInfo["api_model"]] = [];
      for (var i = 0; i < this.ids.length; i++) {
        data[this.moduleInfo["api_model"]].push({
          id: this.ids[i],
          check_status: status,
          check_reason: null,
          check_user_id: "@userId()",
          check_time: "@now()",
        });
      }
      apiPut(data).then((response) => {
        this.$modal.msgSuccess("审批成功");
        this.getList();
      });
    },
    handleCheck(row) {
      this.apply = {
        id: row.id,
        model: this.moduleInfo["api_model"],
        title: "审批 " + this.moduleInfo["name"],
      };
    },
  },
};
</script>
<!--
列表字段的处理逻辑如下：
1、按排序展示字段，可设置列表不展示，具体内容按类型进行格式化
2、普通内容原样展示
3、日期和日期时间自动格式化为 yyyy-MM-dd和yyyy-MM-dd hh:mm:ss
4、支持图片预览
5、支持字典转义
-->
